package Forms;

import Classes.*;
import java.awt.Color;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Owner lklkl
 */
public class AddAdviser extends javax.swing.JFrame {
    static Sections sec;
    static User cUser;
    static MInterface mi;
    static ClassLevel cl;
    static SelectAdviser sa;
    
    ArrayList<User> advisers = null;
    /**
     * Creates new form SelectAdviser
     */
    public AddAdviser(User u, MInterface m, SelectAdviser ss) {
        initComponents();
        this.setLocationRelativeTo(getRootPane());
        
        cUser = u;
        mi = m;
        sa = ss;
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        cancel = new javax.swing.JButton();
        save = new javax.swing.JButton();
        jPanel13 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        unameFld = new javax.swing.JTextField();
        lname = new javax.swing.JTextField();
        gname = new javax.swing.JTextField();
        mname = new javax.swing.JTextField();
        jLabel49 = new javax.swing.JLabel();
        lvlCategory = new javax.swing.JComboBox();
        jLabel86 = new javax.swing.JLabel();
        pwFld = new javax.swing.JPasswordField();
        cPwFld = new javax.swing.JPasswordField();
        jLabel87 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Select Adviser");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        cancel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        save.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        jPanel13.setBackground(new java.awt.Color(255, 255, 255));
        jPanel13.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel12.setText("Username:");

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel15.setText("Last Name:");

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel16.setText("Given Name:");

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel17.setText("Middle Name:");

        jLabel49.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel49.setText("Level Category:");

        lvlCategory.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lvlCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pre-Elementary", "Elementary", "High School" }));
        lvlCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lvlCategoryActionPerformed(evt);
            }
        });

        jLabel86.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel86.setText("Password:");

        pwFld.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        pwFld.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwFldKeyTyped(evt);
            }
        });

        cPwFld.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cPwFld.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cPwFldKeyTyped(evt);
            }
        });

        jLabel87.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel87.setText("Confirm Password:");

        jSeparator1.setForeground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel13Layout.createSequentialGroup()
                                .addComponent(jLabel49)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lvlCategory, 0, 417, Short.MAX_VALUE))
                            .addGroup(jPanel13Layout.createSequentialGroup()
                                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel87)
                                    .addComponent(jLabel86)
                                    .addComponent(jLabel12))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(unameFld)
                                    .addComponent(pwFld)
                                    .addComponent(cPwFld)))
                            .addGroup(jPanel13Layout.createSequentialGroup()
                                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel17)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel15))
                                .addGap(19, 19, 19)
                                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lname)
                                    .addComponent(mname)
                                    .addComponent(gname))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jSeparator1))
                .addContainerGap())
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(unameFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel86)
                    .addComponent(pwFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel87)
                    .addComponent(cPwFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(lname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel16)
                    .addComponent(gname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(mname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel49)
                    .addComponent(lvlCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/banner.jpg"))); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 115, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(save, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jPanel13, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 553, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 144, Short.MAX_VALUE)
                .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancel)
                    .addComponent(save))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addContainerGap(340, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelActionPerformed

    private void lvlCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lvlCategoryActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_lvlCategoryActionPerformed

    private void pwFldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwFldKeyTyped
// TODO add your handling code here:
        /*
         * String s = "!@#$%^&*()-+=~`|/?.>,<][}{;:\'\" ";
         *
         * char c = evt.getKeyChar(); //String s =
         * "-!@#$%^&*()+=~`|/?.>,<][}{;:\'\"";
         *
         * //char c = evt.getKeyChar(); if (s.contains(c + "") || c ==
         * KeyEvent.VK_BACK_SLASH || c == KeyEvent.VK_SPACE || c ==
         * KeyEvent.VK_ENTER) { if (c == KeyEvent.VK_ENTER) {
         * jButton7ActionPerformed(null); } else { evt.consume();
         * JOptionPane.showMessageDialog(null, "Please enter allowed
         * characters.", "Error", JOptionPane.ERROR_MESSAGE);
         * jLabel12.setForeground(Color.red); }
        }
         */

        /*
         * if ("".equals(jPasswordField2.getPassword().toString()) ){
         * jLabel12.setForeground(Color.black); }
         */
        /*
         * if (s.contains(c + "") || c == KeyEvent.VK_BACK_SLASH || c ==
         * KeyEvent.VK_SPACE || c == KeyEvent.VK_ENTER){
         *
         * }
         */

        /*
         * if (!Character.isLetter(c) && c != KeyEvent.VK_SPACE && c !=
         * KeyEvent.VK_BACK_SPACE && !Character.isDigit(c) &&
         * !Character.isSpaceChar(c)){ evt.consume();
         * JOptionPane.showMessageDialog (null, "Please enter allowed
         * characters.", "Error", JOptionPane.ERROR_MESSAGE);
         * jLabel12.setForeground(Color.red); }
         */


  }//GEN-LAST:event_pwFldKeyTyped

    private void cPwFldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cPwFldKeyTyped
// TODO add your handling code here:
        /*
         * String s = "!@#$%^&*()-+=~`|/?.>,<][}{;:\'\" "; char c =
         * evt.getKeyChar();
         *
         * if (s.contains(c + "") || c == KeyEvent.VK_BACK_SLASH || c ==
         * KeyEvent.VK_SPACE || c == KeyEvent.VK_ENTER) { if (c ==
         * KeyEvent.VK_ENTER) { jButton7ActionPerformed(null); } else {
         * evt.consume(); JOptionPane.showMessageDialog(null, "Please enter
         * allowed characters.", "Error", JOptionPane.ERROR_MESSAGE);
         * jLabel17.setForeground(Color.red); }
        }
         */
        /*
         * if (s.contains(c + "") || c == KeyEvent.VK_BACK_SLASH || c ==
         * KeyEvent.VK_SPACE || c == KeyEvent.VK_ENTER){
         *
         * }
         * if (!Character.isLetter(c) && c != KeyEvent.VK_SPACE && c !=
         * KeyEvent.VK_BACK_SPACE && !Character.isDigit(c)){ evt.consume();
         * JOptionPane.showMessageDialog (null, "Please enter allowed
         * characters.", "Error", JOptionPane.ERROR_MESSAGE);
         * jLabel17.setForeground(Color.red); }
         */
    }//GEN-LAST:event_cPwFldKeyTyped

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        boolean exist = false;
        lvlCategory.setEnabled(true);
        String pw1 = new String(pwFld.getPassword());
        String pw2 = new String(cPwFld.getPassword());
        if (!pw1.equals(pw2)) {
            JOptionPane.showMessageDialog(null, "Password does not match", "Error", JOptionPane.ERROR_MESSAGE);
            jLabel86.setForeground(Color.red);
            jLabel87.setForeground(Color.red);
        } else if (pw1.length() < 7) {
            JOptionPane.showMessageDialog(null, "Password too short. Password should contain at least 7 characters", "Error", JOptionPane.ERROR_MESSAGE);
            jLabel86.setForeground(Color.red);
            jLabel87.setForeground(Color.red);
        } else {    
            try {
                boolean addA = false;
                User user = new User(lname.getText().replaceAll("\\s+", " "), gname.getText().replaceAll("\\s+", " "), mname.getText().replaceAll("\\s+", " "), unameFld.getText(), pw1, "Adviser"); 
                exist = mi.add("add-user", user, "", cUser);
                User uu = (User) mi.get("get-user", unameFld.getText(), "");
                
                addA = mi.add("add-classadviser", new Adviser(lvlCategory.getSelectedIndex(), Integer.parseInt(uu.getID())), "", cUser);
                
                if(addA && exist) {
                    JOptionPane.showMessageDialog(null, unameFld.getText() + " successfully added", "Message", JOptionPane.INFORMATION_MESSAGE);
                    sa.loadContent();
                }         
                unameFld.setText("");
                pwFld.setText("");
                cPwFld.setText("");
                lname.setText("");
                gname.setText("");
                mname.setText("");
                unameFld.setEnabled(false);
                pwFld.setEnabled(false);
                cPwFld.setEnabled(false);
                lname.setEnabled(false);
                gname.setEnabled(false);
                mname.setEnabled(false);
                lvlCategory.setEnabled(false);
            } catch (Exception ex) {
                Logger.getLogger(AddAdviser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddAdviser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddAdviser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddAdviser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddAdviser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddAdviser(cUser, mi, sa).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField cPwFld;
    private javax.swing.JButton cancel;
    private javax.swing.JTextField gname;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel49;
    private javax.swing.JLabel jLabel86;
    private javax.swing.JLabel jLabel87;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField lname;
    private javax.swing.JComboBox lvlCategory;
    private javax.swing.JTextField mname;
    private javax.swing.JPasswordField pwFld;
    private javax.swing.JButton save;
    private javax.swing.JTextField unameFld;
    // End of variables declaration//GEN-END:variables
}
